package think.in.spring.bean.builder;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import think.in.spring.bean.factory.UserFactory;
import think.in.spring.ioc.overview.domain.User;

import java.util.ServiceLoader;

import static java.util.ServiceLoader.load;

public class ServiceLoadDemo {
    public static void main(String[] args) throws Exception {
        serviceLoadSpringDemo();
    }

    private static void serviceLoadSpringDemo() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:META-INF/speciallyBeanDefinition.xml");
        ServiceLoader<UserFactory> userFactoryServiceLoad =
                context.getBean("userFactoryServiceLoad", ServiceLoader.class);
        for (UserFactory next : userFactoryServiceLoad) {
            User createUser = next.createUser();
            System.out.println(createUser); 
        }
    }
    private static void serviceLoadDemo() {
        ServiceLoader<UserFactory> load = ServiceLoader.load(UserFactory.class,
                Thread.currentThread().getContextClassLoader());
        for (UserFactory next : load) {
            User createUser = next.createUser();
            System.out.println(createUser);
        }
    }
}
